package com.rivulet.foreign.controller;

import com.rivulet.foreign.pojo.Vacation;
import com.rivulet.foreign.service.ToolService;
import com.rivulet.foreign.service.VacationService;
import com.rivulet.foreign.tools.Lock;
import com.rivulet.foreign.tools.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import static com.rivulet.foreign.tools.Identity.STUDENT;
import static com.rivulet.foreign.tools.Identity.TEACHER;

/**
 * {事假功能}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Controller
@RequestMapping(value = "/vacation")
public class VacationController {
    final
    VacationService vacationService;

    final
    ToolService authorizationForIdentity;

    public VacationController(VacationService vacationService, ToolService authorizationForIdentity) {
        this.vacationService = vacationService;
        this.authorizationForIdentity = authorizationForIdentity;
    }

    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ResponseBody
    public Results apply(
            @RequestBody Vacation vacation,
            @CookieValue(value = "user_id") String student_id
    ) {
        if (!authorizationForIdentity.reAuthorization(student_id, STUDENT)) {
            return null;
        }
        vacation.setUser_id(Lock.lockOff(student_id));
        Results<Vacation> results = vacationService.setVacation(vacation);
        return results;
    }

    @RequestMapping(value = "/checkYet")
    @ResponseBody
    public Results vacationCheckYet(@CookieValue(value = "user_id") String user_id) {
        if (!authorizationForIdentity.reAuthorization(user_id, TEACHER)) {
            return null;
        }
        Results results = vacationService.checkYetVacation();
        return results;
    }

    @RequestMapping(value = "/check")
    public Results vacationCheck(@CookieValue(value = "user_id") String user_id) {
        if (!authorizationForIdentity.reAuthorization(user_id, TEACHER)) {
            return null;
        }
        return new Results(100, "未实现接口");
    }

    @RequestMapping(value = "/deal")
    @ResponseBody
    public Results vacationDeal(@CookieValue("user_id") String user_id, @RequestBody HashMap<String, String> map) {
        if (!authorizationForIdentity.reAuthorization(user_id, TEACHER)) {
            return null;
        }
        Vacation vacation = new Vacation();
        vacation.setUser_id(map.get("user_id"));
        Integer advice = Integer.valueOf(map.get("advice"));
        vacation.setIs_yes(advice);
        Results results = vacationService.dealVacation(Lock.lockOff(user_id), vacation);
        return results;
    }
}
